package com.ruoyi.quartz.task;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.bizsys.domain.ExtMonnifyOrder;
import com.ruoyi.bizsys.domain.LoanIouDeferred;
import com.ruoyi.bizsys.domain.RepaymentOrder;
import com.ruoyi.bizsys.domain.WalletOrder;
import com.ruoyi.bizsys.mapper.ExtMonnifyOrderMapper;
import com.ruoyi.bizsys.mapper.RepaymentOrderMapper;
import com.ruoyi.bizsys.mapper.WalletOrderMapper;
import com.ruoyi.bizsys.service.IWalletOrderService;
import com.ruoyi.business.component.RepaymentMqSendService;
import com.ruoyi.business.domain.WalletOrderReq;
import com.ruoyi.business.service.IDoRepaymentLastService;
import com.ruoyi.business.service.IWalletService;
import com.ruoyi.common.constant.WalletConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import jodd.util.Base64;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 处理解冻钱包的
 * 判断：batch_serial_no和repayment_order中的pay_trans_serial_no匹配。存在，判断是否是失败，失败就解冻，其他的不解冻
 * 不存在，直接解冻
 * @author ruoyi
 */
@Slf4j
@Component("dealWalletOrderUnfreeze")
public class DealWalletOrderUnfreeze {

    @Autowired
    RepaymentOrderMapper repaymentOrderMapper;

    @Autowired
    WalletOrderMapper walletOrderMapper;

    @Autowired
    IWalletOrderService walletOrderService;

    @Autowired
    IWalletService walletService;

    /**
     * sqlStr 参入进行执行
     */
    public void runWalletOrderUnfreezeJob(String id) {
        log.info("处理解冻钱包的-还款：：start");
        if (StringUtils.isNotBlank(id)) {
            log.info("处理解冻钱包的-还款：处理单个：{}", id);
            dealOneData(walletOrderMapper.selectById(id));
            return;
        }
        List<WalletOrder> list = walletOrderMapper.selectListByStatus();
        if (!CollectionUtils.isEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                dealOneData(list.get(i));
            }
        }
    }

    private void dealOneData(WalletOrder walletOrder) {
        RepaymentOrder repaymentOrder = repaymentOrderMapper.getOneByPayTransSerialNo(walletOrder.getBatchSerialNo());
        if (null == repaymentOrder) {
            updateWalletOrder(walletOrder);
            return;
        }
        if (0 == repaymentOrder.getRepayStatus() || 10 == repaymentOrder.getRepayStatus()) {
            updateWalletOrder(walletOrder);
        }
    }

    private void updateWalletOrder(WalletOrder walletOrder) {
        log.info("处理解冻钱包的-还款：处理walletOrder id：{}", walletOrder.getId());
        WalletOrderReq walletOrderReq = WalletOrderReq.builder()
                .orderSerialNo(walletOrder.getBatchSerialNo())
                .approvalStatus(WalletConstants.WALLET_ORDER_STATUS_FAILED)
                .orderType(walletOrder.getOrderType()).build();
        walletService.updateWalletOrder(walletOrderReq);
    }
}